package com.ky.serialport.sensor;


import com.ky.serialport.bean.ComBean;
import com.ky.serialport.utils.MyFunc;

import static com.ky.serialport.utils.RandomUtils.myRandom;

/**
 * 电流电压采集器（隔爆壳）
 */
public class GeBaoKeDataBean {

    public Long time = 0L;
    public int power0 = 0; //电量1
    public int power1 = 0; //电量2
    public int signal = 0; //信号
    public int index = -1;
    public int type = -1;
    public Float[] dianLiuArray = new Float[5]; //可调闸电流
    public Float[] dianYaArray = new Float[5]; //可调闸电压
    public float dianYaVel; //可调闸电压均值
    public float dianLiuVel; //可调闸电流均值

    public GeBaoKeDataBean() {
    }

    public GeBaoKeDataBean(ComBean comBean) {
        caculate(comBean.recData);
        time = comBean.recTime;
    }

    public GeBaoKeDataBean(byte[] recData) {
        caculate(recData);
        time = System.currentTimeMillis();
    }

    public GeBaoKeDataBean caculate(ComBean comBean) {
        time = comBean.recTime;
        return  caculate(comBean.recData);
    }

    public GeBaoKeDataBean caculate(byte[] buffer) {
        power0 = MyFunc.getPower(buffer[38], buffer[39])/2;
//        if (buffer.length == 42) {
//            power0 = MyFunc.getPower(buffer[40], buffer[41]);
//        }
//        signal = MyFunc.getSignal(buffer[buffer.length == 42 ? 40 : 42]); //测试信号
        signal = MyFunc.getSignal(buffer[40]); //测试信号
        type = buffer[9];
        index = buffer[10];

        dianLiuArray[0] = (float) MyFunc.twoByte2IntNoSign(buffer[14], buffer[15]) / 100f;
        dianLiuArray[1] = (float) MyFunc.twoByte2IntNoSign(buffer[16], buffer[17]) / 100f;
        dianLiuArray[2] = (float) MyFunc.twoByte2IntNoSign(buffer[18], buffer[19]) / 100f;
        dianLiuArray[3] = (float) MyFunc.twoByte2IntNoSign(buffer[20], buffer[21]) / 100f;
        dianLiuArray[4] = (float) MyFunc.twoByte2IntNoSign(buffer[22], buffer[23]) / 100f;
        dianLiuVel = (dianLiuArray[0] + dianLiuArray[1] + dianLiuArray[2] + dianLiuArray[3] + dianLiuArray[4]) / 5f;

        dianYaArray[0] = (float) MyFunc.twoByte2IntNoSign(buffer[24], buffer[25]) / 100f;
        dianYaArray[1] = (float) MyFunc.twoByte2IntNoSign(buffer[26], buffer[27]) / 100f;
        dianYaArray[2] = (float) MyFunc.twoByte2IntNoSign(buffer[28], buffer[29]) / 100f;
        dianYaArray[3] = (float) MyFunc.twoByte2IntNoSign(buffer[30], buffer[31]) / 100f;
        dianYaArray[4] = (float) MyFunc.twoByte2IntNoSign(buffer[32], buffer[33]) / 100f;
        dianYaVel = (dianYaArray[0] + dianYaArray[1] + dianYaArray[2] + dianYaArray[3] + dianYaArray[4]) / 5f;
        return this;
    }

    public GeBaoKeDataBean test() {
        time = System.currentTimeMillis();
        power0 = 300 + myRandom.nextInt(120);
        power1 = 300 + myRandom.nextInt(120);
        signal = 200;
        dianLiuVel = myRandom.nextInt(10) + myRandom.nextFloat();
        dianYaVel = myRandom.nextInt(10) + 200;
        return this;
    }

}
